$(document).ready(function () {
    var curPage = $("input[name=page]").val();
    var pageSize = $("input[name=rows]").val();
    var totalCount = $("#totalValue").val();
    $('ul#pagination').page({
        "curPage": parseInt(curPage),
        "pageSize": parseInt(pageSize),
        "totalCount": parseInt(totalCount),
        "init": true,
        "callback": function (page) {
            query(page);
        }
    });

    //初始化查询框
    $("select[name='enabled'], select[name='listType']").select2({
        language: "zh-CN",
        placeholder: '请选择',
        minimumResultsForSearch: -1,
        allowClear: true
    });

});

function query(page) {
    $('#queryForm input:hidden[name=page]').val(page);
    $('#queryForm input:hidden[name=rows]').val($("input[name=rows]").val());
    $('#queryForm').submit();
}

//查询按钮
$('#btnQuery').on({
    click: function () {
        query(1);
    }
});

//重置按钮
$('#btnReset').on({
    click: function () {
        $('#queryForm input,select').not('input:hidden[name=page],input:hidden[name=rows]').val('');
        var enabled = $("select[name='enabled']").select2({
            language: "zh-CN",
            placeholder: '请选择',
            minimumResultsForSearch: -1,
            allowClear: true
        });
        enabled.val('').trigger("change");

        var listType = $("select[name='listType']").select2({
            language: "zh-CN",
            placeholder: '请选择',
            minimumResultsForSearch: -1,
            allowClear: true
        });
        listType.val('').trigger("change");
    }
});

//编辑按钮
$("td a[name='edit']").on({
    click: function () {
        $('#queryForm').attr('action', '/listName/edit');
        $('#queryForm input[name=id]').val($(this).attr("entity-id"));
        $('#queryForm').submit();
    }
});

//新增按钮
$('#btnAdd').on({
    click: function () {
        $('#queryForm').attr('action', '/listName/edit');
        $('#queryForm input[name=id]').val('');
        $('#queryForm').submit();
    }
});